{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 给大家补充几个高级函数\n",
    "# zip\n",
    "- 把两个可迭代内容生成一个可迭代的tuple元素类型组成的内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'zip'>\n",
      "<zip object at 0x7f61c457ab88>\n",
      "(1, 11)\n",
      "(2, 22)\n",
      "(3, 33)\n",
      "(4, 44)\n",
      "(5, 55)\n"
     ]
    }
   ],
   "source": [
    "# zip 案例\n",
    "l1 = [ 1,2,3,4,5]\n",
    "l2 = [11,22,33,44,55]\n",
    "\n",
    "z = zip(l1, l2)\n",
    "\n",
    "print(type(z))\n",
    "print(z)\n",
    "\n",
    "for i in z:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('wangwang', 89)\n",
      "('mingyue', 23)\n",
      "('yyt', 78)\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "l1 = [\"wangwang\", \"mingyue\", \"yyt\"]\n",
    "l2 = [89, 23, 78]\n",
    "\n",
    "z = zip(l1, l2)\n",
    "\n",
    "for i in z:\n",
    "    print(i)\n",
    "    \n",
    "    \n",
    "# 考虑下面结果，为什么会为空\n",
    "l3 = [i for i in z]\n",
    "print(l3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# enumerate \n",
    "- 跟zip功能比较像\n",
    "- 对可迭代对象里的每一元素，配上一个索引，然后索引和内容构成tuple类型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 11), (1, 22), (2, 33), (3, 44), (4, 55)]\n"
     ]
    }
   ],
   "source": [
    "# enumerate案例1\n",
    "l1 = [11,22,33,44,55]\n",
    "\n",
    "em = enumerate(l1)\n",
    "\n",
    "l2 = [i for i in em]\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(100, 11), (101, 22), (102, 33), (103, 44), (104, 55)]\n"
     ]
    }
   ],
   "source": [
    "em = enumerate(l1, start=100)\n",
    "\n",
    "l2 = [ i for i in em]\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# collections模块\n",
    "- namedtuple\n",
    "- deque\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### namedtuple\n",
    "- tuple类型\n",
    "- 是一个可命名的tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "import collections\n",
    "Point = collections.namedtuple(\"Point\", ['x', 'y'])\n",
    "p = Point(11, 22) \n",
    "print(p.x)\n",
    "print(p[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Circle(x=100, y=150, r=50)\n",
      "<class '__main__.Circle'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Circle = collections.namedtuple(\"Circle\", ['x', 'y', 'r'])\n",
    "\n",
    "c = Circle(100, 150, 50)\n",
    "print(c)\n",
    "print(type(c))\n",
    "\n",
    "# 想检测以下namedtuple到底属于谁的子类\n",
    "isinstance(c, tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# dequeue\n",
    "- 比较方便的解决了频繁删除插入带来的效率问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deque(['a', 'b', 'c'])\n",
      "deque(['a', 'b', 'c', 'd'])\n",
      "deque(['x', 'a', 'b', 'c', 'd'])\n"
     ]
    }
   ],
   "source": [
    "from collections import deque\n",
    "\n",
    "q = deque(['a', 'b', 'c'])\n",
    "print(q)\n",
    "\n",
    "q.append(\"d\")\n",
    "print(q)\n",
    "\n",
    "q.appendleft('x')\n",
    "print(q)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# defaultdict\n",
    "- 当直接读取dict不存在的属性时，直接返回默认值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'four'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-27-d54a61646604>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0md1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m\"one\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"two\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"three\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'one'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'four'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'four'"
     ]
    }
   ],
   "source": [
    "d1 = {\"one\":1, \"two\":2, \"three\":3}\n",
    "print(d1['one'])\n",
    "print(d1['four'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "刘大拿\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "# lambda表达式，直接返回字符串\n",
    "func = lambda: \"刘大拿\"\n",
    "d2 = defaultdict(func)\n",
    "\n",
    "d2[\"one\"] = 1\n",
    "d2[\"two\"] = 2\n",
    "\n",
    "print(d2['one'])\n",
    "print(d2['four'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Counter\n",
    "- 统计字符串个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'a': 6, 'b': 5, 'c': 4, 'd': 3, 'e': 2, 'f': 1, 'g': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "\n",
    "# 为什么下面结果不把abcdefgabced.....作为键值，而是以其中每一个字母作为键值\n",
    "# 需要括号里内容为可迭代\n",
    "c = Counter(\"abcdefgabcdeabcdabcaba\")\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'love': 4, 'liudana': 1, 'wangxiaona': 1})\n"
     ]
    }
   ],
   "source": [
    "s = [\"liudana\", \"love\", \"love\", \"love\", \"love\", \"wangxiaona\"]\n",
    "c = Counter(s)\n",
    "\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
